<?php
namespace app\api\controller\v1;
use think\Db;
class Area extends Base
{
	public function _initialize()
	{
		parent::_initialize();
	}

	/**
	 * 获取二级联动地区
	 */
	public function get_area_two_level()
	{
		$list_data = cache('area_two_level_data');
		if(empty($list_data))
		{
			$list = Db::name('area')->field('id, id as value, pid, lev, name, name as label')->where(['lev' => ['ELT', 2], 'is_enable' => 1])->select();
			$list_data = array_tree_structure($list, 0, 'pid', 'children');
			cache('area_two_level_data',$list_data);
		}
		
// 		echo '<pre>';
// 		print_r($list_data);
// 		exit;
		if(!empty($list_data))
		{
			$result['code'] = 0;
			$result['msg'] = '请求成功';
			$result['data'] = $list_data;
		}else
		{
			$result['code'] = -1;
			$result['msg'] = '暂无数据';
		}
		echo json_encode($result);
	}

	/**
	 * 获取三级联动地区
	 */
	public function get_area_three_level()
	{
// 		$list_data = cache('area_three_level_data');
// 		if(empty($list_data))
// 		{
        $ids='';
        $l=db::name('proxy')->where('is_delete_time',0)->select();
        foreach ($l as $k=>$v){
            $ids.=$v['sheng'].','.$v['shi'].','.$v['qu'].',';
        }
			$list = Db::name('area')->field('id, id as value, pid, lev, name, name as label')->where(['id'=>['in',$ids]])->select();
// 			echo Db::name('area')->field('id, id as value, pid, lev, name, name as label')->where(['id'=>['in',$ids]])->fetchsql(true)->select();
			$list_data = array_tree_structure($list, 0, 'pid', 'children');
			cache('area_three_level_data',$list_data);
// 		}
// 				echo '<pre>';
// 		print_r($list_data);
// 		exit;
		if(!empty($list_data))
		{
			$result['code'] = 0;
			$result['msg'] = '请求成功';
			$result['data'] = $list_data;
		}else
		{
			$result['code'] = -1;
			$result['msg'] = '暂无数据';
		}
		echo json_encode($result);
	}

	/**
	 * 获取城市列表
	 */
	public function city_list()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
        
        $city=db::name('techn')->group('city')->column('city');
        $city = Db::name('proxy')->where('is_delete_time',0)->column('shi');
        $xian = Db::name('proxy')->where('is_xian',1)->column('qu');
// 		$list_data = cache('area_city_list_data');
// 		if(empty($list_data))
// 		{
			$list = Db::name('area')->where(['lev' => 2, 'is_enable' => 1,'id'=>['in',$city]])->select();
//            如果有展示的县区,加列表中
            if (!empty($xian))
			$list_qu = Db::name('area')->where(['lev' => 3, 'is_enable' => 1,'id'=>['in',$xian]])->select();
            $list_data_new = [];
            foreach ($list as $kk => $vv){
                $list_data_new[] = $vv;
                foreach ($list_qu as $value){
                    if ($vv['city_code'] == $value['city_code']){
                        $list_data_new[] = $value;
                    }
                }
            }
			cache('area_city_list_data',$list_data_new);
// 		}
		if(!empty($list_data_new))
		{
			$result['code'] = 0;
			$result['msg'] = '请求成功';
			$result['data'] = $list_data_new;
		}else
		{
			$result['code'] = -1;
			$result['msg'] = '即将上线,敬请期待';
		}
		echo json_encode($result);
	}
}